package com.x.utils.http;

import java.io.Serializable;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * Description:用于组装 http get参数 如a=b&b=c ,经过urlencode，key不能重复
 * 
 * 不需要依赖第三方库
 * </p>
 * 
 * @Author Chenkangming
 * @Date 2015年11月5日
 * @version 1.0
 */
public class HttpQueryBuilder implements Serializable {

	private static final long serialVersionUID = -4640130848868785664L;

	private Logger log = LoggerFactory.getLogger(getClass());

	private Map<String, Object> paramsMap = new HashMap<>();

	public HttpQueryBuilder append(String key, String value) {
		this.paramsMap.put(key, value);
		return this;
	}

	public HttpQueryBuilder append(String key, int value) {
		this.paramsMap.put(key, value);
		return this;
	}

	public HttpQueryBuilder append(String key, long value) {
		this.paramsMap.put(key, value);
		return this;
	}

	public HttpQueryBuilder append(String key, double value) {
		this.paramsMap.put(key, value);
		return this;
	}

	/**
	 * return encode a=xx&b=xx&c=xxx
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (Map.Entry<String, Object> entry : this.paramsMap.entrySet()) {
			try {
				sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(), "UTF-8")).append("&");
			} catch (Exception e) {
				log.error("{}", e);
			}
		}
		String text = sb.toString();
		if (text.endsWith("&")) {
			text = text.substring(0, text.length() - 1);
		}
		return text;
	}

	/**
	 * 按升序排序返回
	 * 
	 * @return
	 */
	public String toSortAscString() {
		StringBuffer sb = new StringBuffer();
		Object[] sortArrayParams = this.paramsMap.keySet().toArray();
		Arrays.sort(sortArrayParams);
		for (Object key : sortArrayParams) {
			try {
				sb.append(key).append("=").append(URLEncoder.encode(this.paramsMap.get(key).toString(), "UTF-8")).append("&");
			} catch (Exception e) {
				log.error("{}", e);
			}
		}
		String text = sb.toString();
		if (text.endsWith("&")) {
			text = text.substring(0, text.length() - 1);
		}
		return text;
	}

	/**
	 * 按降序排序返回
	 * 
	 * @return
	 */
	public String toSortDescString() {
		StringBuffer sb = new StringBuffer();
		Object[] sortArrayParams = this.paramsMap.keySet().toArray();
		Arrays.sort(sortArrayParams);
		for (int i = sortArrayParams.length - 1; i >= 0; i--) {
			Object key = sortArrayParams[i];
			try {
				sb.append(key).append("=").append(URLEncoder.encode(this.paramsMap.get(key).toString(), "UTF-8")).append("&");
			} catch (Exception e) {
				log.error("{}", e);
			}
		}
		String text = sb.toString();
		if (text.endsWith("&")) {
			text = text.substring(0, text.length() - 1);
		}
		return text;
	}

	public static void main(String[] args) {
		HttpQueryBuilder hqb = new HttpQueryBuilder();
		hqb.append("a", "a1");
		hqb.append("b", "b1");
		hqb.append("c", "我是中文");
		hqb.append("d", 1000);
		System.out.println(hqb.toString());
		System.out.println(hqb.toSortAscString());
		System.out.println(hqb.toSortDescString());
	}
}
